import axios from 'axios'
import { ElMessage } from 'element-plus'
const service = axios.create({
    baseURL:'http://localhost:8080'
})
// 响应拦截器
service.interceptors.response.use(
  response => {
    const res =  response.data;
    
    // 根据ResultCode处理不同状态
    switch (res.code) {
      case 200: // SUCCESS
        return res.data
      case 401: // UNAUTHORIZED
        router.push('/login?redirect=' + encodeURIComponent(router.currentRoute.value.fullPath))
        return Promise.reject(new Error(res.message || '请重新登录'))
      case 403: // FORBIDDEN
        ElMessage.error(res.message || '没有操作权限')
        return Promise.reject(new Error(res.message || '禁止访问'))
      case 404: // VALIDATE_FAILED
        ElMessage.warning(res.message || '参数验证失败')
        return Promise.reject(new Error(res.message || '参数错误'))
      default:
        ElMessage.error(res.message || '请求错误')
        return Promise.reject(new Error(res.message || 'Error'))
    }
  },
  error => {
    // 处理HTTP错误状态
    if (error.response) {
      switch (error.response.status) {
        case 401:
          router.push('/login')
          break
        case 403:
          ElMessage.error('没有访问权限')
          break
        case 500:
          ElMessage.error('服务器内部错误')
          break
      }
    }
    return Promise.reject(error)
  }
)
export default service;